﻿using UnityEngine;
using System.Collections;

namespace GJXQGame
{
    public class MainWin : GUIBase
    {
        public GameObject SlotObj;
        public GameObject ParantObj;
        public MainWin(GUIManager gMrg)
            : base(gMrg)
        {
            _layer = GUILAYER.PANEL;
            _id = GUI_ID.GUI_ID_MAIN;
            _path = "Windows/Main/MainWin";
        }

        protected override void OnCreate()
        {
            base.OnCreate();
            // UIEventListener.Get(FindChild("Arrow").gameObject).onClick = target => { if (!_scrolling) MainClient.StartCoroutine(ScrollLeft()); };
            //FindChild("GameObject").GetComponent<UIWidget>().width = Screen.width;
            SlotObj = FindChild("ScrollViewSlots/SlotItem").gameObject;
            ParantObj = FindChild("ScrollViewSlots/Grid").gameObject;
            ShowItem();
        }

        private void OnPlay(GameObject Obj)
        {
            Act1Base Act1Base = TableManager.GetInstance().GetAct1Table().Find(ushort.Parse(Obj.name));
            if (Act1Base != null)
            {
                GameManager.ChessDataControl.InitData(Act1Base);
                GameManager.SceneManager.ChangeScene(SCENE_ID.SCENE_BATTLE_GROUND, SCENE_STYLE.MAIN);
            }
        }

        public void ShowItem()
        {
            int Index = 0;
            foreach (Act1Base Act1Base in TableManager.GetInstance().GetAct1Table().MapTable.Values)
            {
                InitItem(Act1Base, Index);
                Index++;
            }
        }

        public void InitItem(Act1Base Act1Base,int Index)
        {
            GameObject NewSlot = GameObject.Instantiate(SlotObj);
            NewSlot.gameObject.SetActive(true);
            NewSlot.name = Act1Base.ID.ToString();
            NewSlot.transform.SetParent(ParantObj.transform);
            NewSlot.transform.localScale = Vector3.one;
            NewSlot.transform.localPosition = new Vector3(0, Index * (-110) + 300, 0);
            NewSlot.transform.FindChild("Title").gameObject.GetComponent<UILabel>().text = ConfigManager.String(Act1Base.namekey);
            UIEventListener.Get(NewSlot).onClick = OnPlay;
        }
    }
}
